﻿/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2012-2018. All rights reserved.
 * Description:
 * Author: 辛爱国 x30005137
 * Create: 2020-07-28
 * Notes: 无
 */
#include "trayicon.h"
#include "settingmenudialog.h"
#include "mainwindow.h"
#include <qapplication.h>
#include <qstyle.h>
#include <qscreen.h>
#include <QDebug>
#include <QString>

TrayIcon::TrayIcon(const QString &Name, QWidget *parent, const int &MenuCount) : QSystemTrayIcon(parent)
{
    m_parent = parent;
    m_MenuCount = MenuCount;
#if defined(IDEASHAREKEY)
    setToolTip(tr("IdeaShare Key"));
#else
    setToolTip(tr("IdeaShare"));
#endif
    this->setIcon(QIcon(":image/images/100.png"));
    m_curStatus = TheFiveButtonType::Connect;
    /* 设置鼠标悬置提示  */
    init();
  
}
void TrayIcon::hideMenu()
{
    if (m_menu) {
        m_menu->hide();
    }
}

QPushButton *TrayIcon::createPushbutton(const QString &Name,const QString &text, QSize size, double rate)
{
    if (m_menu == nullptr || m_vlayout == nullptr) {
        return nullptr;
    }
    QPushButton *button = new QPushButton(m_menu);
    m_vlayout->addWidget(button);
    button->setCursor(QCursor(Qt::PointingHandCursor));
    button->setObjectName(Name);
    button->setText(text);
    button->setFixedSize(size * rate);
    button->style()->unpolish(button);
    button->style()->polish(button);
    button->update();
    QFont font = button->font();
    int fontsize = font.pixelSize() * rate;
    QSize iconsize = button->iconSize() * rate;

    if (fontsize > 0) {
        button->setStyleSheet(QString("qproperty-iconSize:%1px %2px;font-size: %3px;").
                              arg(iconsize.width()).arg(iconsize.height()).arg(fontsize));
    } else {
        button->setStyleSheet(QString("qproperty-iconSize:%1px %2px;").
                              arg(iconsize.width()).arg(iconsize.height()));
    }
    button->installEventFilter(this);
    return button;
}
void TrayIcon::init()
{
    //  分辨率优化
    if (m_menu != nullptr) {
        if (std::fabs(m_rate - getCurrentRate(m_menu)) < 1e-3) {
            return;
        }
    }
    if (m_menu != nullptr) {
        delete m_menu;
        m_menu = nullptr;
    }
    QSize menuSize = QSize(120, 40); // 120菜单按钮宽度 40 菜单按钮高度 
    m_menu = new QMenu(nullptr);
    m_rate = getCurrentRate(m_menu);
    m_menu->setStyleSheet(QString("QMenu{ width:%1px;height:%2px; background: 0XFFFFFF;}").
                          arg(menuSize.width() * m_rate).arg(m_MenuCount * menuSize.height() * m_rate));
    m_menu->setAttribute(Qt::WA_TranslucentBackground);

    m_vlayout = new QVBoxLayout(m_menu);

#if defined(IDEASHAREKEY)                       // 第5个按钮
    m_button = createPushbutton(QString("pushButtonConnect"), QString(tr("Connect")), menuSize, m_rate);
    if (m_button != nullptr) {
        connect(m_button, SIGNAL(clicked()), this, SLOT(slotConnect()));
    }
    showButtonName(m_curStatus);
#endif
    QPushButton *pushButtonSetting = createPushbutton(QString("pushButtonSetting"), QString(tr("Setting")), menuSize, m_rate);
    if (pushButtonSetting != nullptr) {
        connect(pushButtonSetting, SIGNAL(clicked()), this, SLOT(slotSetting()));
    }
    QPushButton *buttonLogs = createPushbutton(QString("pushButtonLogInfo"), QString(tr("Logs")), menuSize, m_rate);
    if (buttonLogs != nullptr) {
        connect(buttonLogs, SIGNAL(clicked()), this, SLOT(slotCollectLog()));
    }
    QPushButton *buttonAbout = createPushbutton(QString("pushButtonAbout"), QString(tr("About")), menuSize, m_rate);
    if (buttonAbout != nullptr) {
        connect(buttonAbout, SIGNAL(clicked()), this, SLOT(slotAbout()));
    }
    QPushButton *buttonExit = createPushbutton(QString("pushButtonMinExit"), QString(tr("Exit")), menuSize, m_rate);
    if (buttonExit != nullptr) {
        connect(buttonExit, SIGNAL(clicked()), this, SLOT(slotExit()));
        buttonExit->setCursor(QCursor(Qt::ArrowCursor));
    }
    m_vlayout->setSpacing(0);
    m_vlayout->setMargin(0);
    m_menu->setLayout(m_vlayout);
    setContextMenu(m_menu);
}

void TrayIcon::slotExit()
{
    hideMenu();
    if (m_parent) {
        qDebug() << "click close signal...";
        //ideasharesdk::auxreq::stop();
        ((MainWindow *)m_parent)->close();
    }
}
void TrayIcon::slotConnect()
{
    if (m_parent) {
        if (m_curStatus == TheFiveButtonType::Connect) {
            hideMenu();
#if defined(IDEASHAREKEY)
            ((MainWindow *)m_parent)->showIdeaShareKey();
#endif
        } else if (m_curStatus == TheFiveButtonType::Share) {
            ((MainWindow *)m_parent)->on_pushButtonShare_clicked();
        } else if (m_curStatus == TheFiveButtonType::Stop) {
            ((MainWindow *)m_parent)->on_pushButtonStop_clicked();
        } else if (m_curStatus == TheFiveButtonType::ShareAudio) {
            ((MainWindow *)m_parent)->on_checkBoxMusic_clicked(true);
        } else if (m_curStatus == TheFiveButtonType::StopShareAudio) {
            ((MainWindow *)m_parent)->on_checkBoxMusic_clicked(false);
        }
    }
}
void TrayIcon::slotCollectLog()
{
    hideMenu();
    if (m_parent) {
        ((MainWindow *)m_parent)->slotpushButtonLogInfo();
    }
}
void TrayIcon::slotAbout()
{
    hideMenu();
    if (m_parent) {
        ((MainWindow *)m_parent)->slotpushButtonAbout();
    }
}
void TrayIcon::slotSetting()
{
    hideMenu();
    if (m_parent) {
        ((MainWindow *)m_parent)->slotpushButtonSetting();
    }
}

bool TrayIcon::MenuisVisible()
{
    return m_menu && m_menu->isVisible();
}
TrayIcon::~TrayIcon()
{
    if (m_menu != nullptr) {
        delete m_menu;
        m_menu = nullptr;
    }
}
void TrayIcon::EnterFun(QObject *watched, QEvent *event)
{
    QString name = watched->objectName();
    if (name == "pushButtonAbout") {
        QIcon icon(":/image/images/67.png");
        ((QPushButton *)watched)->setIcon(icon);
    } else if (name == "pushButtonConnect") {
        QIcon icon(":/image/images/122.png");
        ((QPushButton *)watched)->setIcon(icon);
    } else if (name == "pushButtonSetting") {
        QIcon icon(":/image/images/76.png");
        ((QPushButton *)watched)->setIcon(icon);
    } else if (name == "pushButtonLogInfo") {
        QIcon icon(":/image/images/71.png");
        ((QPushButton *)watched)->setIcon(icon);
    } else if (name == "pushButtonMinExit") {
        QIcon icon(":/image/images/130.png");
        ((QPushButton *)watched)->setIcon(icon);
    } else if (name == "pushButtonShareMenu") {
        QIcon icon(":/image/images/122.png");
        ((QPushButton *)watched)->setIcon(icon);
    } else if (name == "pushButtonStopShareMenu") {
        QIcon icon(":/image/images/122.png");
        ((QPushButton *)watched)->setIcon(icon);
    } else if (name == "pushButtonShareAudio") {
        QIcon icon(":/image/images/128.png");
        ((QPushButton *)watched)->setIcon(icon);
    } else if (name == "pushButtonStopShareAudio") {
        QIcon icon(":/image/images/127.png");
        ((QPushButton *)watched)->setIcon(icon);
    }
}

void TrayIcon::LeaveFun(QObject *watched, QEvent *event)
{
    QString name = watched->objectName();
    if (name == "pushButtonAbout") {
        QIcon icon(":/image/images/70.png");
        ((QPushButton *)watched)->setIcon(icon);
    } else if (name == "pushButtonConnect") {
        QIcon icon(":/image/images/123.png");
        ((QPushButton *)watched)->setIcon(icon);
    } else if (name == "pushButtonSetting") {
        QIcon icon(":/image/images/78.png");
        ((QPushButton *)watched)->setIcon(icon);
    } else if (name == "pushButtonLogInfo") {
        QIcon icon(":/image/images/74.png");
        ((QPushButton *)watched)->setIcon(icon);
    } else if (name == "pushButtonMinExit") {
        QIcon icon(":/image/images/129.png");
        ((QPushButton *)watched)->setIcon(icon);
    } else if (name == "pushButtonShareMenu") {
        QIcon icon(":/image/images/123.png");
        ((QPushButton *)watched)->setIcon(icon);
    } else if (name == "pushButtonStopShareMenu") {
        QIcon icon(":/image/images/123.png");
        ((QPushButton *)watched)->setIcon(icon);
    } else if (name == "pushButtonShareAudio") {
        QIcon icon(":/image/images/131.png");
        ((QPushButton *)watched)->setIcon(icon);
    } else if (name == "pushButtonStopShareAudio") {
        QIcon icon(":/image/images/132.png");
        ((QPushButton *)watched)->setIcon(icon);
    }
}

bool TrayIcon::eventFilter(QObject *watched, QEvent *event)
{    
    if (event->type() == QEvent::Enter) {           
        EnterFun(watched, event);
    } else if (event->type() == QEvent::Leave) {
        LeaveFun(watched, event);
    }
    return QObject::eventFilter(watched, event);
}

#if defined(IDEASHAREKEY)
void TrayIcon::showButtonName(const TheFiveButtonType &status)
{
    if (!m_button)
        return;
    m_curStatus = status;
    switch (status) {
        case TheFiveButtonType::Connect:
            m_button->setObjectName("pushButtonConnect");
            m_button->setText(tr("Connect"));
            break;
        case TheFiveButtonType::Share:
            m_button->setObjectName("pushButtonShareMenu");
            m_button->setText(tr("Share"));
            break;
        case TheFiveButtonType::Stop:
            m_button->setObjectName("pushButtonStopShareMenu");
            m_button->setText(tr("Stop"));
            break;
        case TheFiveButtonType::ShareAudio:
            m_button->setObjectName("pushButtonShareAudio");
            m_button->setText(tr("Cast Audio"));
            break;
        case TheFiveButtonType::StopShareAudio:
            m_button->setObjectName("pushButtonStopShareAudio");
            m_button->setText(tr("Stop Audio"));
            break;
        default:
            break;
    }
    m_button->style()->unpolish(m_button);
    m_button->style()->polish(m_button);
    m_button->update();
}
#endif
